package com.code.webshopapp;

import java.text.SimpleDateFormat;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class DetailSeite extends Activity {

	private static long idProduct;
	private static String title;
	private static String imageUrl;
	private static double rating;
	private static long createTime;
	private static double price;
	private static String description;
	private static String feedback;

	private final static int maxRating = 5;

	private ImageCache imageCache;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_detail_anzeige);

		// ////// Caching
		final int cacheSize = (int) ((Runtime.getRuntime().maxMemory() / 1024)) / 8;
		imageCache = new ImageCache(cacheSize);
		// ////////

		Intent intentReceived = getIntent();
		idProduct = intentReceived.getIntExtra("id", 0);
		title = intentReceived.getStringExtra("title");
		imageUrl = intentReceived.getStringExtra("imageUrl");
		rating = intentReceived.getDoubleExtra("rating", 0);
		createTime = intentReceived.getLongExtra("createTime", 0);
		price = intentReceived.getDoubleExtra("price", 1);
		description = intentReceived.getStringExtra("description");
		feedback = intentReceived.getStringExtra("feedback");

		// / Produktanzeige aktualisieren
		TextView txtTitle = (TextView) findViewById(R.id.detailTitel);
		TextView txtRating = (TextView) findViewById(R.id.detailBewertung);
		TextView txtCreateTime = (TextView) findViewById(R.id.detailcreateTime);
		TextView txtPrice = (TextView) findViewById(R.id.detailPrice);
		TextView txtDescription = (TextView) findViewById(R.id.detailBeschreibung);
		TextView txtFeedback = (TextView) findViewById(R.id.detailFeedback);
		ImageView detailImage = (ImageView) findViewById(R.id.detailImage);

		String createTimeString = new SimpleDateFormat("MM/dd/yyyy HH:mm").format(createTime);

		txtTitle.setText(title);
		txtRating.setText("Rating: " + String.valueOf(rating));
		txtCreateTime.setText("Created on: " + createTimeString);
		txtPrice.setText(price + " $");
		txtDescription.setText(description);
		txtFeedback.setText(feedback);

		View linRatingView = findViewById(R.id.detailBewertungLinear);
		int ratingInt = (int) Math.round(rating);

		for (int i = 0; i < maxRating; i++) {
			ImageView img = new ImageView(this);
			img.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
					LayoutParams.WRAP_CONTENT));

			if (ratingInt > i) {
				img.setImageResource(R.drawable.ruby_full);
			} else {
				img.setImageResource(R.drawable.ruby_empty);
			}
			((ViewGroup) linRatingView).addView(img);
		}

		if (imageCache.getBitmapFromMemCache(imageUrl) != null) {
			detailImage.setImageBitmap(imageCache
					.getBitmapFromMemCache(imageUrl));
		} else {
			try {
				Bitmap bmp = new ImageDownloader().execute(imageUrl).get();
				detailImage.setImageBitmap(bmp);
			} catch (Exception e) {
			}
		}

		TextView login = (TextView) findViewById(R.id.detailLoginTextView);
		login.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent(getApplicationContext(), Login.class);
				startActivity(intent);
			}
		});

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_start_seite, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (item.getItemId() == R.id.menuBeenden) {
			Toast.makeText(this, "Bitte geh noch nicht :_)", Toast.LENGTH_SHORT)
					.show();
		}
		if (item.getItemId() == R.id.menuLogin) {
			Intent intent = new Intent(getApplicationContext(), Login.class);
			startActivity(intent);
		}

		return true;
	}

}
